<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$this->model('model')->checkuser();
if($_RQ['op']=='main'){
	$store_id=intval($_RQ['storeid']);
	$this->store_id=$store_id;
	$lessonid=intval($_RQ['lessonid']);
	$isfee=intval($_RQ['isfee']);
	$dayid=intval($_RQ['dayid']);
	if(empty($lessonid)){
		exi('请先选择课程！','error','goback');
	}
	//下单页
	$item=array();
	//提交数据
	$return['post'] = array(
		'storeid'=>$store_id,
		'isfee'=>$isfee,
		'lessonid'=>$lessonid,
		'dayid'=>$dayid
	);
	$return['store']=pdo_get('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	$return['store']['area_title']=pdo_getcolumn('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid'],'id'=>$return['store']['area_id']),'title');
	if($lessonid){
		$data=array();
		$data['0']=date("Y-m-d",time("day")); 
		$data['1']=date("Y-m-d",strtotime("+1 day")); 
		$data['2']=date("Y-m-d",strtotime("+2 day")); 
		$data['3']=date("Y-m-d",strtotime("+3 day")); 
		$data['4']=date("Y-m-d",strtotime("+4 day")); 
		$data['5']=date("Y-m-d",strtotime("+5 day")); 
		$data['6']=date("Y-m-d",strtotime("+6 day"));
		$return['lesson']=pdo_get('jy_kexie_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonid));
		$return['lesson']['onday']=$this->model('lesson')->get_onday($lessonid);
		$return['day']=$data[$dayid];
		if($isfee){
			$return['lesson']['price']=0.00;
		}
		$classid=$return['lesson']['classid'];
		$lesson_pai=pdo_getall('jy_kexie_lesson_pai',array('lessonid'=>$lessonid,'uniacid'=>$_SESSION['uniacid']));
		$store_list=array();
		foreach($lesson_pai as $lv){
			$store=pdo_get('jy_kexie_store',array('id'=>$lv['storeid']));
			$store['pcd']=json_decode($store['pcd'],true);
			$store['week_days']=$this->week_days[$store['week_days']];
			$store_list[$lv['storeid']]=$store;
		}
		$store_list=array_values($store_list);
		$return['store_list']=$store_list;
		$return['store']=$return['store']?:$store_list[0];
	}
	$user=pdo_get('jy_kexie_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$user['name']=$user['name']?:$_SESSION['member']['nickname'];
	$return['post']['name']=$user['name'];
	set_html('title','确认订单');
	exi($return);
}

// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['lessonid']){
		exi('请选择课程！','error');
	}
	if(!$post['name']){
		exi('请填写报名人！','error');
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'storeid'=>intval($post['storeid']),
		'lesson_id'=>intval($post['lessonid']),
		'worker_id'=>intval($post['worker_id']),
		'remark'=>trim($post['remark']),
		'diyformdata'=>ijson_encode($post['diyformdata']),
		'name'=>trim($post['name']),
		'mobile'=>trim($post['mobile']),
		'sex'=>intval($post['sex']),
	);
	if($post['isfee']){
		$new_order['money']=0;
	}else{
		$new_order['money']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$new_order['lesson_id']),'price');
	}
	model('jy_kexie/lesson')->add_lesson_log($new_order);
	if($new_order['money']>0){
		$order_id=model('jy_kexie/order')->create($new_order,'',$url);
		$params=array(
			'id'=>$order_id,
			'title'=>'报名上课',
			'fee'=>$new_order['money'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_kexie/create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_kexie/order/detail/id='.$order_id,
			'error_url'=>'jy_kexie/order/detail/id='.$order_id,
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_kexie_order',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
		);
		exi($res);
	}else{
		exi('报名成功！','','/mylesson_log/');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_kexie/order/');
	}
	$order=pdo_get('jy_kexie_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_kexie/order/');
	}
	if($order['pay']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['pay'].'|'.$payresult['fee'],'error','jy_kexie/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
	);
	$url=custom_url('jy_kexie/order/detail/id='.$order['id'],true);
	model('jy_kexie/order')->order_pay($order['id'],$params,$url);
	exi('支付成功!');
}

?>